package com.itheima.user.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.entity.ApUser;
import com.heima.model.userDto.LoginDto;
import com.heima.utils.common.AppJwtUtil;
import com.itheima.user.dao.ApUserDao;
import com.itheima.user.service.ApUserService;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jcajce.provider.asymmetric.ec.GMCipherSpi;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * APP用户表(ApUser)表服务实现类
 *
 * @author makejava
 * @since 2022-04-18 18:16:32
 */
@Service("apUserService")
public class ApUserServiceImpl implements ApUserService {
    @Resource
    private ApUserDao apUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ApUser queryById(Long id) {
        return this.apUserDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param apUser 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ApUser> queryByPage(ApUser apUser, PageRequest pageRequest) {
        long total = this.apUserDao.count(apUser);
        return new PageImpl<>(this.apUserDao.queryAllByLimit(apUser, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param apUser 实例对象
     * @return 实例对象
     */
    @Override
    public ApUser insert(ApUser apUser) {
        this.apUserDao.insert(apUser);
        return apUser;
    }

    /**
     * 修改数据
     *
     * @param apUser 实例对象
     * @return 实例对象
     */
    @Override
    public ApUser update(ApUser apUser) {
        this.apUserDao.update(apUser);
        return this.queryById(apUser.getId());
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.apUserDao.deleteById(id) > 0;
    }


    @Override
    public ResponseResult login(LoginDto loginDto) {
        //1先判断是否为 null
        if (StringUtils.isNotBlank(loginDto.getPhone())&&StringUtils.isNotBlank(loginDto.getPassword())) {
            //用户登录
           ApUser apUser= apUserDao.queryByPhone(loginDto.getPhone());
           if (apUser == null) {
               return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
           }
        //获取用户 密码进行盐值 加起来 进行加密计算
            String password = loginDto.getPassword()+apUser.getSalt();
           //进行加密
            String s = DigestUtils.md5DigestAsHex(password.getBytes());
            if (StringUtils.equals(s,apUser.getPassword())){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //等于密码 将id根据jwt生成token
            String token = AppJwtUtil.getToken(apUser.getId());
            Map<String, Object> result = new HashMap<>();
            result.put("token",token);
            apUser.setPassword("");
            apUser.setSalt("");
            result.put("user",apUser);
            return ResponseResult.okResult(result);
        }
        //游客用户登录
        Map<String, Object> result = new HashMap<>();
        result.put("token",AppJwtUtil.getToken(0L));
        return ResponseResult.okResult(result);
    }
}
